package com.itboye.lottery.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.itboye.lottery.R;
import com.itboye.lottery.bean.NoticeBean;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by wu on 2018/1/9.
 */

public class AnnouncementAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    ArrayList<NoticeBean.NoticeItem> classEntityArrayList = new ArrayList<>();
    Fragment fragment;

    public AnnouncementAdapter(Fragment fragment, ArrayList<NoticeBean.NoticeItem> classEntityArrayList) {
        this.fragment = fragment;
        this.classEntityArrayList = classEntityArrayList;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(AnnouncementAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolderAnnouncement(LayoutInflater.from(context).inflate(R.layout.item_announcement, parent, false));
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NoticeBean.NoticeItem noticeItem = classEntityArrayList.get(position);
        ViewHolderAnnouncement viewHolderAnnouncement = ((ViewHolderAnnouncement) holder);
        viewHolderAnnouncement.item_announcement_ll_number.removeAllViews();
        if (noticeItem.getRed_num() != null) {
            for (int i = 0; i < noticeItem.getRed_num().size(); i++) {
                viewHolderAnnouncement.item_announcement_ll_number.addView(createNumBall(noticeItem.getRed_num().get(i), true));
            }
        }
        if (noticeItem.getBlue_num() != null) {
            for (int i = 0; i < noticeItem.getBlue_num().size(); i++) {
                viewHolderAnnouncement.item_announcement_ll_number.addView(createNumBall(noticeItem.getBlue_num().get(i), false));
            }
        }
        viewHolderAnnouncement.item_tv_announcement_title.setText(noticeItem.getName());
        viewHolderAnnouncement.item_tv_announcement_qishu_time.setText("第" + noticeItem.getNumber() + "期 " + noticeItem.getOpen_time());
        String cateId = noticeItem.getCategory_id();
        Object content = noticeItem.getContent();
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
        String jsonString = gson.toJson(content);
        setViewType(viewHolderAnnouncement, 0);

        viewHolderAnnouncement.item_tv_announcement_one_bqc.setTextColor(context.getResources().getColor(R.color.red));
        viewHolderAnnouncement.item_tv_announcement_one_zjq.setTextColor(context.getResources().getColor(R.color.red));
        try {
            if (cateId != null) {
                switch (cateId) {
                    case "1001"://双色球
                        setViewType(viewHolderAnnouncement, 1);
                        JSONArray jsonArrayColorBall = new JSONArray(jsonString);
                        viewHolderAnnouncement.item_tv_announcement_one_zhushu.setText(jsonArrayColorBall.getJSONArray(0).get(1) + "");
                        viewHolderAnnouncement.item_tv_announcement_two_zhushu.setText(jsonArrayColorBall.getJSONArray(1).get(1) + "");
                        viewHolderAnnouncement.item_tv_announcement_one_jiangjin.setText(Html.fromHtml("<font color='red'>" + jsonArrayColorBall.getJSONArray(0).get(2) + "</font>元"));
                        viewHolderAnnouncement.item_tv_announcement_two_jiangjin.setText(Html.fromHtml("<font color='red'>" + jsonArrayColorBall.getJSONArray(1).get(2) + "</font>元"));
                        break;
                    case "2001"://大乐透
                        setViewType(viewHolderAnnouncement, 1);
                        JSONObject jsonArrayDaLeTou = new JSONObject(jsonString);
                        viewHolderAnnouncement.item_tv_announcement_one_zhushu.setText(jsonArrayDaLeTou.getJSONObject("一").getJSONArray("j").get(0) + "");
                        viewHolderAnnouncement.item_tv_announcement_two_zhushu.setText(jsonArrayDaLeTou.getJSONObject("二").getJSONArray("j").get(0) + "");
                        viewHolderAnnouncement.item_tv_announcement_one_jiangjin.setText(Html.fromHtml("<font color='red'>" + jsonArrayDaLeTou.getJSONObject("一").getJSONArray("j").get(1) + "</font>元"));
                        viewHolderAnnouncement.item_tv_announcement_two_jiangjin.setText(Html.fromHtml("<font color='red'>" + jsonArrayDaLeTou.getJSONObject("二").getJSONArray("j").get(1) + "</font>元"));
                        break;
                    case "0"://足球
                    case "3010":
                        viewHolderAnnouncement.item_tv_announcement_qishu_time.setText(classEntityArrayList.get(position).getNum() + " " + classEntityArrayList.get(position).getOpen_time());
                        //显示主队，比分，客队
                        setViewType(viewHolderAnnouncement, 2);
                        viewHolderAnnouncement.item_tv_announcement_first_prize.setText(classEntityArrayList.get(position).getWin());//胜平负
                        //让球（胜负）
                        viewHolderAnnouncement.item_tv_announcement_one_zhushu.setText("(" + classEntityArrayList.get(position).getLet_score() + ")" + classEntityArrayList.get(position).getJieguo());//
                        viewHolderAnnouncement.item_tv_announcement_one_jiangjin.setText(classEntityArrayList.get(position).getFinal_score());//比分
                        viewHolderAnnouncement.item_tv_announcement_one_jiangjin.setTextColor(context.getResources().getColor(R.color.red));
                        viewHolderAnnouncement.item_tv_announcement_one_zhushu.setTextColor(context.getResources().getColor(R.color.red));
                        viewHolderAnnouncement.item_tv_announcement_first_prize.setTextColor(context.getResources().getColor(R.color.red));
                        viewHolderAnnouncement.item_tv_announcement_one_zjq.setText("" + classEntityArrayList.get(position).getZong());//总进球
                        viewHolderAnnouncement.item_tv_announcement_one_bqc.setText(classEntityArrayList.get(position).getBanquan());//半全场
                        viewHolderAnnouncement.item_announcement_h.setText(classEntityArrayList.get(position).getH_cn_abbr());
                        viewHolderAnnouncement.item_announcement_a.setText(classEntityArrayList.get(position).getA_cn_abbr());
                        viewHolderAnnouncement.item_announcement_score.setText(classEntityArrayList.get(position).getFinal_score());
                        break;
                    case "3001"://蓝球
                        setViewType(viewHolderAnnouncement, 3);
                        viewHolderAnnouncement.item_announcement_h.setText(classEntityArrayList.get(position).getH_cn_abbr());
                        viewHolderAnnouncement.item_announcement_a.setText(classEntityArrayList.get(position).getA_cn_abbr());
                        viewHolderAnnouncement.item_announcement_score.setText(classEntityArrayList.get(position).getFinal_score());
                        viewHolderAnnouncement.item_tv_announcement_qishu_time.setText(noticeItem.getNum() + noticeItem.getOpen_time());


                        viewHolderAnnouncement.item_tv_announcement_first_prize.setText(TextUtils.isEmpty(noticeItem.getWin()) ? "-" : noticeItem.getWin());
                        viewHolderAnnouncement.item_tv_announcement_one_zhushu.setText(TextUtils.isEmpty(noticeItem.getLet_score()) ? "-" : noticeItem.getLet_score());
                        viewHolderAnnouncement.item_tv_announcement_one_jiangjin.setText(noticeItem.getZong() == 0 ? "-" : noticeItem.getZong() + "");
                        viewHolderAnnouncement.item_tv_announcement_one_zjq.setText(TextUtils.isEmpty(noticeItem.getVictory()) ? "-" : noticeItem.getVictory());
                        viewHolderAnnouncement.item_tv_announcement_one_bqc.setTextColor(Color.parseColor("#737373"));
                        viewHolderAnnouncement.item_tv_announcement_one_zjq.setTextColor(Color.parseColor("#737373"));
//                        if (TextUtils.isEmpty(noticeItem.getJieguo())) {
//                            viewHolderAnnouncement.item_tv_announcement_first_prize.setText("-");
//                            viewHolderAnnouncement.item_tv_announcement_one_zhushu.setText("-");
//                            viewHolderAnnouncement.item_tv_announcement_one_jiangjin.setText("-");
//                            viewHolderAnnouncement.item_tv_announcement_one_zjq.setText("-");
//                        }else{
//
//                        }
                        break;
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void setViewType(ViewHolderAnnouncement holderAnnouncement, int type) {
        //0：重置所有  type:1数字彩   2：竞彩
        if (type == 1) {
            holderAnnouncement.hs_num_root.setVisibility(View.VISIBLE);
            holderAnnouncement.ll_jincai_root.setVisibility(View.GONE);
            holderAnnouncement.item_tv_announcement_two_zhushu.setVisibility(View.VISIBLE);
            holderAnnouncement.item_tv_announcement_two_jiangjin.setVisibility(View.VISIBLE);
            holderAnnouncement.tv_title_1.setText("奖项");
            holderAnnouncement.tv_title_2.setText("中奖注数");
            holderAnnouncement.tv_title_3.setText("每注奖金");
        } else if (type == 2) {
            holderAnnouncement.ll_jincai_root.setVisibility(View.VISIBLE);
            holderAnnouncement.ll_jincai_root_2.setVisibility(View.VISIBLE);
            holderAnnouncement.ll_jincai_root_3.setVisibility(View.VISIBLE);
            holderAnnouncement.hs_num_root.setVisibility(View.GONE);
            holderAnnouncement.item_tv_announcement_second_prize.setVisibility(View.GONE);
            holderAnnouncement.item_tv_announcement_two_zhushu.setVisibility(View.GONE);
            holderAnnouncement.item_tv_announcement_two_jiangjin.setVisibility(View.GONE);
            holderAnnouncement.tv_title_1.setText("胜负");
            holderAnnouncement.tv_title_2.setText("让球");
            holderAnnouncement.tv_title_3.setText("比分");
        } else if (type == 3) {
            holderAnnouncement.ll_jincai_root.setVisibility(View.VISIBLE);
            holderAnnouncement.ll_jincai_root_2.setVisibility(View.VISIBLE);
            holderAnnouncement.ll_jincai_root_3.setVisibility(View.VISIBLE);
            holderAnnouncement.hs_num_root.setVisibility(View.GONE);
            holderAnnouncement.item_tv_announcement_second_prize.setVisibility(View.GONE);
            holderAnnouncement.item_tv_announcement_two_zhushu.setVisibility(View.GONE);
            holderAnnouncement.item_tv_announcement_two_jiangjin.setVisibility(View.GONE);
            holderAnnouncement.tv_title_1.setText("胜负");
            holderAnnouncement.tv_title_2.setText("让分");
            holderAnnouncement.tv_title_3.setText("大小分");
            holderAnnouncement.tv_title_4.setText("胜负差");
            holderAnnouncement.ll_jincai_root_3.setVisibility(View.GONE);
        } else if (type == 0) {
            holderAnnouncement.ll_jincai_root.setVisibility(View.GONE);
            holderAnnouncement.hs_num_root.setVisibility(View.GONE);
            holderAnnouncement.ll_jincai_root_2.setVisibility(View.GONE);
            holderAnnouncement.ll_jincai_root_3.setVisibility(View.GONE);
        }
    }

    private TextView createNumBall(String text, boolean isRed) {
        TextView textView = new TextView(context);
        textView.setText(text);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(context.getResources().getColor(R.color.white));
        textView.setBackgroundDrawable(context.getResources().getDrawable(isRed ? R.drawable.solid_circle_border_red : R.drawable.solid_circle_border_blue));
//                textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.hollow_circle_border_red));
//                textView.setTextColor(context.getResources().getColor(R.color.main_color));
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(4, 4, 0, 4);
        textView.setLayoutParams(layoutParams);
        return textView;
    }

    @Override
    public int getItemCount() {
        return classEntityArrayList == null ? 0 : classEntityArrayList.size();
    }

    public class ViewHolderAnnouncement extends RecyclerView.ViewHolder implements View.OnClickListener {
        public LinearLayout item_announcement_ll_number;
        TextView item_tv_announcement_title, item_tv_announcement_qishu_time;
        TextView item_tv_announcement_one_jiangjin, item_tv_announcement_two_jiangjin, item_tv_announcement_one_zhushu, item_tv_announcement_two_zhushu, item_tv_announcement_first_prize, item_tv_announcement_second_prize, item_tv_announcement_one_zjq, item_tv_announcement_one_bqc;
        LinearLayout ll_jincai_root, ll_jincai_root_2, ll_jincai_root_3;
        HorizontalScrollView hs_num_root;
        TextView tv_title_1, tv_title_2, tv_title_3, tv_title_4, tv_title_5;
        TextView item_announcement_h, item_announcement_a, item_announcement_score;

        public ViewHolderAnnouncement(View itemView) {
            super(itemView);
            item_tv_announcement_title = itemView.findViewById(R.id.item_tv_announcement_title);
            item_tv_announcement_qishu_time = itemView.findViewById(R.id.item_tv_announcement_qishu_time);
            item_announcement_ll_number = itemView.findViewById(R.id.item_announcement_ll_number);
            item_tv_announcement_one_jiangjin = itemView.findViewById(R.id.item_tv_announcement_one_jiangjin);
            item_tv_announcement_two_jiangjin = itemView.findViewById(R.id.item_tv_announcement_two_jiangjin);
            item_tv_announcement_one_zhushu = itemView.findViewById(R.id.item_tv_announcement_one_zhushu);
            item_tv_announcement_two_zhushu = itemView.findViewById(R.id.item_tv_announcement_two_zhushu);
            item_tv_announcement_first_prize = itemView.findViewById(R.id.item_tv_announcement_first_prize);
            item_tv_announcement_second_prize = itemView.findViewById(R.id.item_tv_announcement_second_prize);
            item_tv_announcement_one_bqc = itemView.findViewById(R.id.item_tv_announcement_one_bqc);
            item_tv_announcement_one_zjq = itemView.findViewById(R.id.item_tv_announcement_one_zjq);
            ll_jincai_root = itemView.findViewById(R.id.ll_jincai_root);
            hs_num_root = itemView.findViewById(R.id.hs_num_root);
            ll_jincai_root_2 = itemView.findViewById(R.id.ll_jincai_root_2);
            ll_jincai_root_3 = itemView.findViewById(R.id.ll_jincai_root_3);
            tv_title_1 = itemView.findViewById(R.id.tv_title_1);
            tv_title_2 = itemView.findViewById(R.id.tv_title_2);
            tv_title_3 = itemView.findViewById(R.id.tv_title_3);
            tv_title_4 = itemView.findViewById(R.id.tv_title_4);
            tv_title_5 = itemView.findViewById(R.id.tv_title_5);
            item_announcement_h = itemView.findViewById(R.id.item_announcement_h);
            item_announcement_a = itemView.findViewById(R.id.item_announcement_a);
            item_announcement_score = itemView.findViewById(R.id.item_announcement_score);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

}
